package com.reggie.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.configuration.common.LocalThread;
import com.reggie.configuration.common.Result;
import com.reggie.order.entity.OrderDetail;
import com.reggie.order.entity.Orders;
import com.reggie.order.service.OrderDetailService;
import com.reggie.order.service.OrderService;
import com.reggie.setmeal.entity.Setmeal;
import com.reggie.shoppingcart.entity.ShoppingCart;
import com.reggie.shoppingcart.service.ShoppingCartService;
import com.reggie.user.entity.User;
import com.reggie.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private UserService userService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return Result.success("下单成功");
    }

    @GetMapping("userPage")
    public Result<IPage> userList(Integer page,Integer pageSize){
        IPage<Orders> pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);

        IPage<Orders> result = orderService.page(pageInfo,ordersLambdaQueryWrapper);

        return Result.success(result);
    }

    @GetMapping("page")
    public Result<IPage> employeeList(Integer page,Integer pageSize){

        IPage<Orders> pageInfo = new Page(page, pageSize);

        IPage<Orders> result = orderService.page(pageInfo);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        LambdaQueryWrapper<User> lambda = queryWrapper.lambda();

        List<Orders> collect = result.getRecords().stream().peek(item -> {
            LambdaQueryWrapper<User> eq = lambda.eq(User::getId, item.getUserId());
            User one = userService.getOne(eq);
            item.setUserName(one.getName());

        }).collect(Collectors.toList());

        result.setRecords(collect);

        return Result.success(result);
    }


    @PutMapping
    public Result<String> orderStatus(@RequestBody Orders map){

        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();

        final LambdaQueryWrapper<Orders> lambda = queryWrapper.lambda();

        lambda.eq(Orders::getId,map.getId());

        final Orders one = orderService.getOne(queryWrapper);
        one.setStatus(map.getStatus());
        orderService.updateById(one);

        return Result.success("派送成功");
    }

    @PostMapping("again")
    public Result<String> again(@RequestBody Orders orders){
        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();

        LambdaQueryWrapper<OrderDetail> lambda = queryWrapper.lambda();

        lambda.eq(OrderDetail::getOrderId,orders.getId());
        //获取到了上个订单的详细菜品id
        List<OrderDetail> list = orderDetailService.list(queryWrapper);
        //将上个订单的菜品添加进购物车（看看可不可以把之前对购物车直接搬过来）

        List<ShoppingCart> collect = list.stream().map(item -> {
            //这个对象是准备，然后从order——de那张表里查询菜品对数据，再次添加到购物车里要很多，是个集合，等会用map映射
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(item, shoppingCart);
            //设置用户id，指定当前是哪个用户的购物车数据
            Long currentId = LocalThread.getUserThread().getId();
            shoppingCart.setUserId(currentId);
            return shoppingCart;
        }).collect(Collectors.toList());

        shoppingCartService.saveBatch(collect);

        return Result.success("ok");
    }


}